<?php


namespace app\live\controller\v3;


use app\chat\LiveMessage;
use app\common\cachemodel\live\LiveAnchorInfo;
use app\common\cachemodel\live\LiveAnswerAstroRecord;
use app\common\cachemodel\live\LiveAnswerDiceRecord;
use app\common\cachemodel\live\LiveAnswerTarotRecord;
use app\common\cachemodel\live\LiveOrderData;
use app\common\cachemodel\live\LiveRecordingInfo;
use app\common\cachemodel\Orders;
use app\common\cachemodel\ZlTarotRecords;
use app\common\cachemodel\ZlUserVip;
use app\common\lib\exception\ApiException;
use app\common\lib\Push;
use app\common\lib\Util;
use app\common\model\Divination;
use app\common\model\MessageCenter;
use app\common\model\User;
use app\live\controller\LiveAuth;
use think\Env;
use think\response\Json;

class LiveAnswer extends LiveAuth
{
    /**
     * 获取星盘解答设置
     */
    public function getAstroAnswerConfig()
    {   // 获取最新的直播记录ID
        $liveRecordId = (new LiveRecordingInfo())->getNewestRecord($this->liveInfo['live_id']);

        $resConfig = (new LiveAnchorInfo())->getAnswerConfig($this->liveInfo['live_id'], $liveRecordId, 'astro');

        return show(config('code.success'), '请求成功', $resConfig);
    }

    /**
     * 更新 星盘解答设置
     */
    public function setAstroAnswerConfig()
    {
        $switch = input('switch/d', 99999);
        $type = input('type/d', 0);
        $price = input('price/f', '');
        $isFreeChooseRecode = input('is_free_choose_recode/d', 99999);
        $isAllowSynastry = input('is_allow_synastry/d', 99999);
        $maxPeopleNum = input('max_people_num/d', 0);
        $questionRequire = input('question_require/s', '');
        $peopleAuth = input('people_auth/d', 0);
        $liveAnchorInfoModel = new LiveAnchorInfo();
        $liveAnswerModel = new LiveAnswerAstroRecord();

        $updateData = [];
        if (in_array($switch, [1, 0], true))
        {
            $updateData['switch'] = $switch;
        }

        if (in_array($type, [1, 2], true))
        {
            $updateData['type'] = $type;
            if ($type === 2)
            {
                if (empty($price) || !is_numeric($price) || $price <= 0)
                {
                    return show(config('code.error'), '付费金额异常');
                }
                $updateData['price'] = $price;
            }
        }

        if (in_array($isFreeChooseRecode, [1, 0], true))
        {
            $updateData['is_free_choose_recode'] = $isFreeChooseRecode;
        }

        if (in_array($isAllowSynastry, [1, 0], true))
        {
            $updateData['is_allow_synastry'] = $isAllowSynastry;
        }

        if ($maxPeopleNum)
        {
            if ($maxPeopleNum > 50 || $maxPeopleNum < 1)
            {
                return show(config('code.error'), '申请人数最多支持设置1-50人');
            }

            $updateData['max_people_num'] = $maxPeopleNum;
        }

        if ($questionRequire)
        {
            if (mb_strlen($questionRequire) > 42)
            {
                return show(config('code.error'), '看盘问题要求最多42字');
            }
            $updateData['question_require'] = $questionRequire;
        }

        if (in_array($peopleAuth, [1, 2, 3], true))
        {
            $updateData['people_auth'] = $peopleAuth;
        }

        // 获取最新的直播记录ID
        $liveRecordId = (new LiveRecordingInfo())->getNewestRecord($this->liveInfo['live_id']);

        $resConfig = $liveAnchorInfoModel->getAnswerConfig($this->liveInfo['live_id'], $liveRecordId, 'astro');

        if ($updateData)
        {
            if (isset($updateData['is_free_choose_recode'])
                || isset($updateData['is_allow_synastry'])
                || isset($updateData['max_people_num'])
                || isset($updateData['question_require'])
                || isset($updateData['people_auth'])
            )
            {   // 如果是 解答 权限或者 解答 用户数量的变更
                if ((int)$resConfig['switch'] === 1)
                {
                    return show(config('code.error'), '只能在功能关闭的情况下修改设置');
                }
                $config = [
                    'is_free_choose_recode' => $updateData['is_free_choose_recode'] ?? $resConfig['is_free_choose_recode'],
                    'is_allow_synastry' => $updateData['is_allow_synastry'] ?? $resConfig['is_allow_synastry'],
                    'max_people_num' => $updateData['max_people_num'] ?? $resConfig['max_people_num'],
                    'question_require' => $updateData['question_require'] ?? $resConfig['question_require'],
                    'people_auth' => $updateData['people_auth'] ?? $resConfig['people_auth'],
                ];
                try
                {
                    $liveAnchorInfoModel->setAnchorExtendConfig($this->liveInfo['live_id'], 'astro_config', $config);
                }
                catch (\Exception $e)
                {
                    return show(config('code.error'), '设置失败', $resConfig);
                }

                $resConfig = array_merge($resConfig, $config);
            }

            if (isset($updateData['type']))
            {
                if ((int)$resConfig['switch'] === 1)
                {
                    return show(config('code.error'), '只能在功能关闭的情况下修改设置');
                }
                if (isset($updateData['price']) && $resConfig['price'] > 0 && $updateData['price'] > 0 && (float)$updateData['price'] !== (float)$resConfig['price'])
                {
                    return show(config('code.error'), '一次直播只支持设置1次价格');
                }
                $config1 = [
                    'type' => $updateData['type'] ?? $resConfig['type'],
                    'price' => $updateData['type'] === 2 ? $updateData['price'] ?? $resConfig['price'] : $resConfig['price'],   // 付费的情况 更新价格
                ];
                try
                {
                    $liveAnchorInfoModel->setAnswerPayInfo($this->liveInfo['live_id'], $liveRecordId, 'astro', $config1);
                }
                catch (\Exception $e)
                {
                    return show(config('code.error'), '设置失败', $resConfig);
                }
                $resConfig = array_merge($resConfig, $config1);
            }

            if (isset($updateData['switch']))
            {   // 更新开关
                $resConfig['switch'] = $updateData['switch'];
                if ($resConfig['switch'] === 1)
                {
                    if (empty($resConfig['question_require']))
                    {
                        return show(config('code.error'), '只有设置了看盘问题要求才能开启看盘功能');
                    }

                    $nowAnswerData = $liveAnswerModel->getNowAnswerDataByCache($liveRecordId);
                    if ($nowAnswerData)
                    {
                        return show(config('code.error'), '正在解答中，开启看盘功能失败～');
                    }

                    if ((int)$resConfig['type'] === 1)
                    {   // 开启 免费
                        // 获取付费的申请列表
                        $answerList = $liveAnswerModel->getRequestAnswerUserList($this->liveInfo['live_id'], $liveRecordId, 2);
                        // 如果付费的申请列表 还有申请并未处理完 则不允许开启免费的申请
                        if ($answerList)
                        {
                            return show(config('code.error'), '付费的看盘申请还未处理完，不允许开启免费看盘');
                        }
                    } else
                    {   // 付费
                        // 获取免费的申请列表
                        $answerList = $liveAnswerModel->getRequestAnswerUserList($this->liveInfo['live_id'], $liveRecordId, 1);
                        // 如果付费的申请列表 还有申请并未处理完 则 取消全部的免费申请
                        if ($answerList)
                        {
                            //  遍历取消当前 免费看盘申请
                            $liveAnswerModel->cancelAllAnswerList((int)$this->liveInfo['live_id'], (int)$liveRecordId, 1, $answerList);
                        }
                    }

                    LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_APP_USER, $this->liveInfo['live_id'], $this->liveInfo['live_id'])
                        , LiveMessage::MSG_TYPE_POP_LIVE_ANSWER_OPEN, [
                            'type' => 1,
                            'pay_type' => (int)$resConfig['type'],
                            'toast' => '当前页面已失效，请重试～'
                        ]);
                }

                $liveAnchorInfoModel->setAnswerSwitchInfo($this->liveInfo['live_id'], $liveRecordId, 'astro', $resConfig['switch']);
            }

            return show(config('code.success'), '设置成功', $resConfig);
        } else
        {
            return show(config('code.error'), '设置失败', $resConfig);
        }
    }

    /**
     * 获取骰子解答设置
     */
    public function getDiceAnswerConfig()
    {
        // 获取最新的直播记录ID
        $liveRecordId = (new LiveRecordingInfo())->getNewestRecord($this->liveInfo['live_id']);

        $resConfig = (new LiveAnchorInfo())->getAnswerConfig($this->liveInfo['live_id'], $liveRecordId, 'dice');

        return show(config('code.success'), '请求成功', $resConfig);
    }

    /**
     * 更新 骰子解答设置
     */
    public function setDiceAnswerConfig()
    {
        $switch = input('switch/d', 99999);
        $type = input('type/d', 0);
        $price = input('price/s', '');
        $maxPeopleNum = input('max_people_num/d', 0);
        $questionRequire = input('question_require/s', '');
        $peopleAuth = input('people_auth/d', 0);
        $liveAnchorInfoModel = new LiveAnchorInfo();
        $liveAnswerModel = new LiveAnswerDiceRecord();
        $updateData = [];
        if (in_array($switch, [1, 0], true))
        {
            $updateData['switch'] = $switch;
        }

        if (in_array($type, [1, 2], true))
        {
            $updateData['type'] = $type;
            if ($type === 2)
            {
                if (empty($price) || !is_numeric($price) || $price <= 0)
                {
                    return show(config('code.error'), '付费金额异常');
                }
                $updateData['price'] = $price;
            }
        }

        if ($maxPeopleNum)
        {
            if ($maxPeopleNum > 50 || $maxPeopleNum < 1)
            {
                return show(config('code.error'), '申请人数最多支持设置1-50人');
            }

            $updateData['max_people_num'] = $maxPeopleNum;
        }

        if ($questionRequire)
        {
            if (mb_strlen($questionRequire) > 42)
            {
                return show(config('code.error'), '骰子问题要求最多42字');
            }
            $updateData['question_require'] = $questionRequire;
        }

        if (in_array($peopleAuth, [1, 2, 3], true))
        {
            $updateData['people_auth'] = $peopleAuth;
        }

        // 获取最新的直播记录ID
        $liveRecordId = (new LiveRecordingInfo())->getNewestRecord($this->liveInfo['live_id']);

        $resConfig = $liveAnchorInfoModel->getAnswerConfig($this->liveInfo['live_id'], $liveRecordId, 'dice');

        if ($updateData)
        {
            if (isset($updateData['max_people_num'])
                || isset($updateData['question_require'])
                || isset($updateData['people_auth'])
            )
            {   // 如果是解答权限或者 解答用户数量的变更
                $config = [
                    'max_people_num' => $updateData['max_people_num'] ?? $resConfig['max_people_num'],
                    'question_require' => $updateData['question_require'] ?? $resConfig['question_require'],
                    'people_auth' => $updateData['people_auth'] ?? $resConfig['people_auth'],
                ];
                if ((int)$resConfig['switch'] === 1)
                {
                    return show(config('code.error'), '只能在功能关闭的情况下修改设置');
                }
                try
                {
                    $liveAnchorInfoModel->setAnchorExtendConfig($this->liveInfo['live_id'], 'dice_config', $config);
                }
                catch (\Exception $e)
                {
                    return show(config('code.error'), '设置失败', $resConfig);
                }

                $resConfig = array_merge($resConfig, $config);
            }

            if (isset($updateData['type']))
            {
                if ((int)$resConfig['switch'] === 1)
                {
                    return show(config('code.error'), '只能在功能关闭的情况下修改设置');
                }
                if (isset($updateData['price']) && $resConfig['price'] > 0 && $updateData['price'] > 0 && (float)$updateData['price'] !== (float)$resConfig['price'])
                {
                    return show(config('code.error'), '一次直播只支持设置1次价格');
                }
                $config1 = [
                    'type' => $updateData['type'] ?? $resConfig['type'],
                    'price' => $updateData['price'] ?? $resConfig['price'],
                ];
                try
                {
                    $liveAnchorInfoModel->setAnswerPayInfo($this->liveInfo['live_id'], $liveRecordId, 'dice', $config1);
                }
                catch (\Exception $e)
                {
                    return show(config('code.error'), '设置失败', $resConfig);
                }
                $resConfig = array_merge($resConfig, $config1);
            }

            if (isset($updateData['switch']))
            {   // 更新开关
                $resConfig['switch'] = $updateData['switch'];
                if ($resConfig['switch'] === 1)
                {
                    if (empty($resConfig['question_require']))
                    {
                        return show(config('code.error'), '只有设置了骰子问题要求才能开启骰子功能');
                    }

                    $nowAnswerData = $liveAnswerModel->getNowAnswerDataByCache($liveRecordId);
                    if ($nowAnswerData)
                    {
                        return show(config('code.error'), '正在解答中，开启骰子功能失败～');
                    }

                    if ((int)$resConfig['type'] === 1)
                    {   // 开启 免费
                        // 获取付费的申请列表
                        $answerList = $liveAnswerModel->getRequestAnswerUserList($this->liveInfo['live_id'], $liveRecordId, 2);
                        // 如果付费的申请列表 还有申请并未处理完 则不允许开启免费的申请
                        if ($answerList)
                        {
                            return show(config('code.error'), '付费的骰子申请还未处理完，不允许开启免费骰子');
                        }
                    } else
                    {   // 付费
                        // 获取免费的申请列表
                        $answerList = $liveAnswerModel->getRequestAnswerUserList($this->liveInfo['live_id'], $liveRecordId, 1);
                        // 如果付费的申请列表 还有申请并未处理完 则 取消全部的免费申请
                        if ($answerList)
                        {
                            // 遍历取消当前 免费看盘申请
                            $liveAnswerModel->cancelAllAnswerList((int)$this->liveInfo['live_id'], (int)$liveRecordId, 2, $answerList);
                        }
                    }

                    LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_APP_USER, $this->liveInfo['live_id'], $this->liveInfo['live_id'])
                        , LiveMessage::MSG_TYPE_POP_LIVE_ANSWER_OPEN, [
                            'type' => 2,
                            'pay_type' => (int)$resConfig['type'],
                            'toast' => '当前页面已失效，请重试～'
                        ]);
                }

                $liveAnchorInfoModel->setAnswerSwitchInfo($this->liveInfo['live_id'], $liveRecordId, 'dice', $resConfig['switch']);
            }

            return show(config('code.success'), '设置成功', $resConfig);
        } else
        {
            return show(config('code.error'), '设置失败', $resConfig);
        }
    }

    /**
     * 获取塔罗解答设置
     */
    public function getTarotAnswerConfig()
    {
        // 获取最新的直播记录ID
        $liveRecordId = (new LiveRecordingInfo())->getNewestRecord($this->liveInfo['live_id']);

        $resConfig = (new LiveAnchorInfo())->getAnswerConfig($this->liveInfo['live_id'], $liveRecordId, 'tarot');

        return show(config('code.success'), '请求成功', $resConfig);
    }

    /**
     * 更新 塔罗解答设置
     */
    public function setTarotAnswerConfig()
    {
        $switch = input('switch/d', 99999);
        $type = input('type/d', 0);
        $price = input('price/s', '');
        $maxPeopleNum = input('max_people_num/d', 0);
        $questionRequire = input('question_require/s', '');
        $peopleAuth = input('people_auth/d', 0);
        $liveAnchorInfoModel = new LiveAnchorInfo();
        $liveAnswerModel = new LiveAnswerTarotRecord();

        $updateData = [];
        if (in_array($switch, [1, 0], true))
        {
            $updateData['switch'] = $switch;
        }

        if (in_array($type, [1, 2], true))
        {
            $updateData['type'] = $type;
            if ($type === 2)
            {
                if (empty($price) || !is_numeric($price) || $price <= 0)
                {
                    return show(config('code.error'), '付费金额异常');
                }
                $updateData['price'] = $price;
            }
        }

        if ($maxPeopleNum)
        {
            if ($maxPeopleNum > 50 || $maxPeopleNum < 1)
            {
                return show(config('code.error'), '申请人数最多支持设置1-50人');
            }

            $updateData['max_people_num'] = $maxPeopleNum;
        }

        if ($questionRequire)
        {
            if (mb_strlen($questionRequire) > 42)
            {
                return show(config('code.error'), '塔罗问题要求最多42字');
            }
            $updateData['question_require'] = $questionRequire;
        }

        if (in_array($peopleAuth, [1, 2, 3], true))
        {
            $updateData['people_auth'] = $peopleAuth;
        }

        // 获取最新的直播记录ID
        $liveRecordId = (new LiveRecordingInfo())->getNewestRecord($this->liveInfo['live_id']);

        $resConfig = $liveAnchorInfoModel->getAnswerConfig($this->liveInfo['live_id'], $liveRecordId, 'tarot');

        if ($updateData)
        {
            if (isset($updateData['max_people_num'])
                || isset($updateData['question_require'])
                || isset($updateData['people_auth'])
            )
            {
                if ((int)$resConfig['switch'] === 1)
                {
                    return show(config('code.error'), '只能在功能关闭的情况下修改设置');
                }
                // 如果是解答权限或者 解答用户数量的变更
                $config = [
                    'max_people_num' => $updateData['max_people_num'] ?? $resConfig['max_people_num'],
                    'question_require' => $updateData['question_require'] ?? $resConfig['question_require'],
                    'people_auth' => $updateData['people_auth'] ?? $resConfig['people_auth'],
                ];
                try
                {
                    $liveAnchorInfoModel->setAnchorExtendConfig($this->liveInfo['live_id'], 'tarot_config', $config);
                }
                catch (\Exception $e)
                {
                    return show(config('code.error'), '设置失败', $resConfig);
                }

                $resConfig = array_merge($resConfig, $config);
            }

            if (isset($updateData['type']))
            {
                if ((int)$resConfig['switch'] === 1)
                {
                    return show(config('code.error'), '只能在功能关闭的情况下修改设置');
                }
                if (isset($updateData['price']) && $resConfig['price'] > 0 && $updateData['price'] > 0 && (float)$updateData['price'] !== (float)$resConfig['price'])
                {
                    return show(config('code.error'), '一次直播只支持设置1次价格');
                }
                $config1 = [
                    'type' => $updateData['type'] ?? $resConfig['type'],
                    'price' => $updateData['price'] ?? $resConfig['price'],
                ];
                try
                {
                    $liveAnchorInfoModel->setAnswerPayInfo($this->liveInfo['live_id'], $liveRecordId, 'tarot', $config1);
                }
                catch (\Exception $e)
                {
                    return show(config('code.error'), '设置失败', $resConfig);
                }
                $resConfig = array_merge($resConfig, $config1);
            }

            if (isset($updateData['switch']))
            {   // 更新开关
                $resConfig['switch'] = $updateData['switch'];
                if ($resConfig['switch'] === 1)
                {
                    if (empty($resConfig['question_require']))
                    {
                        return show(config('code.error'), '只有设置了塔罗问题要求才能开启塔罗功能');
                    }

                    $nowAnswerData = $liveAnswerModel->getNowAnswerDataByCache($liveRecordId);
                    if ($nowAnswerData)
                    {
                        return show(config('code.error'), '正在解答中，开启塔罗功能失败～');
                    }

                    if ((int)$resConfig['type'] === 1)
                    {   // 开启 免费
                        // 获取付费的申请列表
                        $answerList = $liveAnswerModel->getRequestAnswerUserList($this->liveInfo['live_id'], $liveRecordId, 2);
                        // 如果付费的申请列表 还有申请并未处理完 则不允许开启免费的申请
                        if ($answerList)
                        {
                            return show(config('code.error'), '付费的塔罗申请还未处理完，不允许开启免费塔罗');
                        }
                    } else
                    {   // 付费
                        // 获取免费的申请列表
                        $answerList = $liveAnswerModel->getRequestAnswerUserList($this->liveInfo['live_id'], $liveRecordId, 1);
                        // 如果付费的申请列表 还有申请并未处理完 则 取消全部的免费申请
                        if ($answerList)
                        {
                            // 遍历取消当前 免费看盘申请
                            $liveAnswerModel->cancelAllAnswerList((int)$this->liveInfo['live_id'], (int)$liveRecordId, 3, $answerList);
                        }
                    }

                    LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_APP_USER, $this->liveInfo['live_id'], $this->liveInfo['live_id'])
                        , LiveMessage::MSG_TYPE_POP_LIVE_ANSWER_OPEN, [
                            'type' => 3,
                            'pay_type' => (int)$resConfig['type'],
                            'toast' => '当前页面已失效，请重试～'
                        ]);
                }
                $liveAnchorInfoModel->setAnswerSwitchInfo($this->liveInfo['live_id'], $liveRecordId, 'tarot', $resConfig['switch']);
            }

            return show(config('code.success'), '设置成功', $resConfig);
        } else
        {
            return show(config('code.error'), '设置失败', $resConfig);
        }
    }

    /**
     * 获取 申请解答 列表
     * @return Json
     * @throws \Exception
     */
    public function getRequestAnswerUserList()
    {
        $type = input('type/d', 0);  //0全部用户|申请中  1=>下单   2=>会员  3=>已完成  4=>已取消
        $questionType = input('question_type/d', 0);    // 问题类型 1=>看盘，2=>骰子，3=>塔罗

        if (!in_array($type, [0, 1, 2, 3, 4], true) || !in_array($questionType, [1, 2, 3], true))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        // 获取最新的直播记录ID
        $liveRecordId = (new LiveRecordingInfo())->getNewestRecord($this->liveInfo['live_id']);

        $liveAnswerModel = null;
        $configName = '';
        switch ($questionType)
        {
            case 1:
                $liveAnswerModel = new LiveAnswerAstroRecord();
                $configName = 'astro';
                break;
            case 2:
                $liveAnswerModel = new LiveAnswerDiceRecord();
                $configName = 'dice';
                break;
            case 3:
                $liveAnswerModel = new LiveAnswerTarotRecord();
                $configName = 'tarot';
                break;
        }

        $resConfig = (new LiveAnchorInfo())->getAnswerConfig($this->liveInfo['live_id'], $liveRecordId, 'astro');

        if (in_array($type, [0, 1, 2]))
        {
            $answerList = $liveAnswerModel->getRequestAnswerUserList($this->liveInfo['live_id'], $liveRecordId, 2);
            $requestAnswerIds = [];
            if ($answerList)
            {   // 付费
                $payType = 2;   // 付费类型
                $requestAnswerIds = array_values($answerList);
            } else
            {
                $answerList = $liveAnswerModel->getRequestAnswerUserList($this->liveInfo['live_id'], $liveRecordId, 1);
                if ($answerList)
                {   // 免费
                    $payType = 1;
                    if ($type === 1)
                    {   // 下单
                        $orderUids = (new LiveOrderData())->getLiveRecordOrderUid($liveRecordId);
                        foreach ($answerList as $uid => $reqId)
                        {
                            if (in_array($uid, $orderUids, false))
                            {
                                $requestAnswerIds[] = (int)$reqId;
                            }
                        }
                    } elseif ($type === 2)
                    {   // VIP
                        foreach ($answerList as $uid => $reqId)
                        {
                            $vipLevel = (new ZlUserVip())->getUserVipLevel($uid);
                            if ($vipLevel > 0)
                            {   // 是VIP
                                $requestAnswerIds[] = (int)$reqId;
                            }
                        }
                    } else
                    {
                        $requestAnswerIds = array_values($answerList);
                    }
                } else
                {
                    $resConfig = (new LiveAnchorInfo())->getAnswerConfig($this->liveInfo['live_id'], $liveRecordId, $configName);
                    $payType = (int)$resConfig['type']; // 1=>免费 2=>付费
                }
            }

        } else
        {
            // 付费 已完成｜已结束
            $payType = 2;
            // 已完成 ｜已结束
            $requestAnswerIds = array_values($liveAnswerModel->getResAnswerUserList($liveRecordId, $type - 2));
        }
        $resRequestList = []; //申请列表
        $resNowRequestList = [];//正在解答中的申请
        if ($requestAnswerIds)
        {   // 有用户申请
            $requestAnswerIds = array_map(function ($v)
            {
                return (int)$v;
            }, $requestAnswerIds);
            $requestAnsertData = $requestAnswerIds ? $liveAnswerModel->batchGetInfoByIds($requestAnswerIds) : [];
            // 排序
            if ($payType === 2)
            {
                //完成和取消根据完成时间倒叙
                if (in_array($type, [3, 4], true))
                {
                    $sortArr = array_column($requestAnsertData, 'cancel');
                    $sortType = SORT_DESC;

                } //根据创建时间正叙
                else
                {
                    $sortArr = array_column($requestAnsertData, 'create_time');
                    $sortType = SORT_ASC;
                }
                array_multisort($sortArr, SORT_NUMERIC, $sortType, $requestAnsertData);
            }//根据创建时间正叙
            else
            {
                $sortArr = array_column($requestAnsertData, 'create_time');
                array_multisort($sortArr, SORT_NUMERIC, SORT_ASC, $requestAnsertData);
            }
            // 获取当前解答 信息
            $nowAnswerInfo = $liveAnswerModel->getNowAnswerDataByCache($liveRecordId);
            // 直播间在线用户列表
            $clientUserList = LiveMessage::getUidListByGroup($this->liveInfo['live_id'] . '_app_user_group');
            foreach ($requestAnsertData as $ansertDatum)
            {
                // 当前 解答 状态 0=>待解答    1=>邀请等待用户接受 2=>用户接受开始解答中 3=>解答结束 4=>用户取消解答 5=>主播取消解答
                if ($nowAnswerInfo && (int)$nowAnswerInfo['id'] === (int)$ansertDatum['id'])
                {
                    $answerStatus = 2;
                    $resNowRequestList[] = [
                        'nickname' => $ansertDatum['nickname'],
                        'avatar' => $ansertDatum['avatar'],
                        'id' => (int)$ansertDatum['id'],
                        'online_status' => in_array($ansertDatum['uid'], $clientUserList, false) ? 1 : 0,
                        'request_time' => Sec2Time1($this->request->time() - $ansertDatum['create_time']),
                        'status' => $answerStatus,
                        'price' => $ansertDatum['price'],
                        'question' => $ansertDatum['question'],
                    ];
                    continue;
                }

                if ($ansertDatum['status'] == 3)
                {
                    $answerStatus = 1;
                } elseif ($ansertDatum['status'] == 6)
                {
                    // 连麦状态  3=>解答结束 4=>用户取消解答 5=>主播取消解答
                    $answerStatus = 3;
                } elseif ($ansertDatum['status'] == 9)
                {
                    $answerStatus = 5;
                } elseif (in_array($ansertDatum['status'], [7, 8, 10], false))
                {
                    $answerStatus = 4;
                } else
                {
                    $answerStatus = 0;
                }

                $resRequestList[] = [
                    'nickname' => $ansertDatum['nickname'],
                    'avatar' => $ansertDatum['avatar'],
                    'id' => (int)$ansertDatum['id'],
                    'online_status' => in_array($ansertDatum['uid'], $clientUserList, false) ? 1 : 0,
                    'request_time' => Sec2Time1($this->request->time() - $ansertDatum['create_time']),
                    'status' => $answerStatus,
                    'price' => $ansertDatum['price'],
                    'question' => $ansertDatum['question'],
                ];
            }
        }
        $resRequestList = array_merge($resNowRequestList, $resRequestList);
        return show(config('code.success'), '请求成功', [
            'switch' => (int)$resConfig['switch'],
            'request_list' => $resRequestList,
            'style_type' => $payType
        ]);
    }

    /**
     * 修改解答状态
     */
    public function alterAnswerInfo()
    {
        $id = input('id/d', 0);  // 申请记录ID
        $status = input('status/d', 0); // 1=>开始解答，2=>完成解答，3=>取消解答
        $reasonId = input('reason_id/d', 0);    // 取消原因
        $type = input('type/d', 0);    // 问题类型 1=>看盘，2=>骰子，3=>塔罗

        if (!$id || !in_array($status, [1, 2, 3], true) || !in_array($type, [1, 2, 3], true))
        {
            return show(config('code.error'), '请求异常，请稍后再试～', ['status' => 2]);
        }
        // 获取最新的直播记录ID
        $liveRecordId = (new LiveRecordingInfo())->getNewestRecord($this->liveInfo['live_id']);
        switch ($type)
        {
            case 1:
                $liveAnswerModel = new LiveAnswerAstroRecord();
                $answerTypeName = 'astro';
                $needAuth = 1;
                $answerName = '看盘';
                $orderType = 19;
                break;
            case 2:
                $liveAnswerModel = new LiveAnswerDiceRecord();
                $answerTypeName = 'dice';
                $needAuth = 2;
                $answerName = '骰子';
                $orderType = 20;
                break;
            default:
                $liveAnswerModel = new LiveAnswerTarotRecord();
                $answerTypeName = 'tarot';
                $needAuth = 2;
                $orderType = 21;
                $answerName = '塔罗';
        }

        $answerInfo = $liveAnswerModel->getInfoById($id);

        if (!$answerInfo || (int)$answerInfo['live_id'] !== $this->liveInfo['live_id'])
        {
            return show(config('code.error'), '服务异常，申请解答信息不存在～', ['status' => 2]);
        }
        $answerInfo['status'] = (int)$answerInfo['status'];
        switch ($status)
        {
            case 1: // 开始解答
                $nowAnswerData = $liveAnswerModel->getNowAnswerDataByCache($liveRecordId);
                if ($nowAnswerData)
                {
                    return show(config('code.error'), '正在解答中，开始解答失败～', ['status' => 2]);
                }
                switch ($answerInfo['status'])
                {
                    case 1:
                        return show(config('code.error'), '服务异常，申请解答信息不存在～', ['status' => 2]);
                    case 3:
                        return show(config('code.error'), '等待用户响应中，操作失败～', ['status' => 2]);
                    case 5:
                        return show(config('code.error'), '正在解答中，操作失败～', ['status' => 2]);
                    case 6:
                        return show(config('code.error'), '解答申请已完成，操作失败～', ['status' => 2]);
                    case 7:
                    case 8:
                    case 9:
                        return show(config('code.error'), '解答申请已取消，操作失败～', ['status' => 2]);
                    default:
                }

                if ($answerInfo['price'] > 0)
                {   // 付费
                    if (!LiveMessage::getUidIsOnline($this->liveInfo['live_id'], $answerInfo['uid']))
                    {// 用户不在线
                        return show(config('code.error'), '用户已离线，开始解答失败～', ['status' => 2]);
                    }
                    $payType = 2;//付费
                    $answerStatus = 3;  // 等待响应
                    $startTime = 0;  // 开始解答时间  还未开始
                } else
                {   // 免费
                    $payType = 1;//免费
                    $answerStatus = 5;  // 解答中
                    $startTime = $this->request->time();  // 开始解答时间
                }

                // 更新解答状态
                $liveAnswerModel->updateAnswerRequest([
                    'id' => $id,
                    'status' => $answerStatus,
                    'start_time' => $startTime,
                ]);

                $nowAnswerData = [
                    'id' => $id, // 解答ID    ID==0 为邀请解答  否则为主播接受解答
                    'answer_sign' => uniqid(),
                    'pay_type' => $payType,  // 1=>免费 2=>付费
                    'live_id' => $this->liveInfo['live_id'],
                    'uid' => $answerInfo['uid'],
                    'nickname' => $answerInfo['nickname'] ?? '',
                    'avatar' => $answerInfo['avatar'] ?? '',
                    'uen' => $answerInfo['uen'] ?? '',
                    'status' => $answerStatus,  // 当前解答状态 状态  1=>待支付 2=>等待解答｜排队中  3=>待响应 4=>未响应 5=>解答中 6=>解答完成 7=>超时取消 8=>用户主动取消 9=>主播取消
                    'request_time' => $this->request->time(),   // 邀请用户解答时间
                ];

                $resStatus = $liveAnswerModel->saveNowAnswerDataToCache($liveRecordId, $nowAnswerData);

                if (!$resStatus)
                {
                    return show(config('code.error'), '服务异常，解答失败', ['status' => 2]);
                }
                // ws 消息    给用户端发送消息
                LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_UID, $this->liveInfo['live_id'], $answerInfo['uid'])
                    , LiveMessage::MSG_TYPE_POP_ANSWER_REFRESH, [
                        'target' => 'local',
                        'link' => "live_{$answerTypeName}?id={$id}&live_record_id={$liveRecordId}&need_auth={$needAuth}",
                        'type' => $type,
                        'force_open' => 1
                    ]);

                LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_LIVER, $answerInfo['live_id'], $answerInfo['live_id'])
                    , LiveMessage::MSG_TYPE_POP_LIVE_ANSWER_ICON, [
                        'icon' => "/live/answer/{$answerTypeName}_icon.png",
                        'hint' => $payType === 1 ? '解答中' : '待响应',
                        'type' => $type,
                        'id' => $id,
                        'status' => 1,
                    ]);
                if ($payType === 1)
                {   // 免费时
                    $answerList = $liveAnswerModel->getRequestAnswerUserList($this->liveInfo['live_id'], $liveRecordId, $payType);
                    foreach ($answerList as $uid => $reqId)
                    {
                        if ($uid != $answerInfo['uid'])
                        {
                            // 向直播间其他 申请用户
                            LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_UID, $this->liveInfo['live_id'], $uid)
                                , LiveMessage::MSG_TYPE_POP_ANSWER_REFRESH, [
                                    'target' => 'local',
                                    'link' => "live_{$answerTypeName}?id={$reqId}&live_record_id={$liveRecordId}&need_auth={$needAuth}",
                                    'type' => $type,
                                    'force_open' => 0
                                ]);
                        }
                    }
                }
                // 发送响应 超时队列
                if ($payType === 2)
                {   // 30秒后执行
                    Util::queuePush('live_answer_client_timeout_check', [
                        'answer_sign' => $nowAnswerData['answer_sign'],
                        'live_record_id' => $liveRecordId,
                        'type' => $type // 问题类型 1=>看盘，2=>骰子，3=>塔罗
                    ], 30);
                }

                $data = $this->getAnswerDetail($id, $type);

                $res = [
                    'id' => $id,
                    'status' => $data['status'],
                    'continue_sencond' => $data['continue_sencond'],
                    'uen' => $data['uen'],
                    'style_type' => (float)$data['price'] > 0 ? 2 : 1,//1=>免费。2=>付费
                ];

                return show(config('code.success'), '请求成功', $res);
            case 2: // 完成解答
                switch ($answerInfo['status'])
                {
                    case 1:
                        return show(config('code.error'), '服务异常，申请解答信息不存在～', ['status' => 2]);
                    case 2:
                    case 4:
                        return show(config('code.error'), '当前申请还未开始解答，操作失败～', ['status' => 2]);
                    case 3:
                        return show(config('code.error'), '等待用户响应中，操作失败～', ['status' => 2]);
                    case 6:
                        return show(config('code.error'), '解答申请已完成，操作失败～', ['status' => 2]);
                    case 7:
                    case 8:
                    case 9:
                        return show(config('code.error'), '解答申请已取消，操作失败～', ['status' => 2]);
                    default:
                }

                // 计算解答时长
                $duration = $this->request->time() - $answerInfo['start_time'];

                // 更新解答状态
                $liveAnswerModel->updateAnswerRequest([
                    'id' => $id,
                    'status' => 6,
                    'duration' => $duration,
                    'cancel' => $this->request->time(),
                    'answer_end_rid' => $liveRecordId
                ]);

                if ($answerInfo['price'] > 0)
                {   // 付费
                    // 更新订单信息
                    (new Orders())->UpdateData($answerInfo['order_num'], ['status' => 4]);

                    $payType = 2;//付费
                } else
                {
                    $payType = 1;//免费
                }

                //ws 消息
                LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_UID, $this->liveInfo['live_id'], $answerInfo['uid'])
                    , LiveMessage::MSG_TYPE_POP_ANSWER_REFRESH, [
                        'target' => 'local',
                        'link' => "live_{$answerTypeName}?id={$id}&live_record_id={$liveRecordId}&need_auth={$needAuth}",
                        'type' => $type,
                        'force_open' => 1
                    ]);
                $answerList = $liveAnswerModel->getRequestAnswerUserList($this->liveInfo['live_id'], $liveRecordId, $payType);
                foreach ($answerList as $uid => $reqId)
                {
                    if ($uid != $answerInfo['uid'])
                    {
                        // 向直播间其他 申请用户
                        LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_UID, $this->liveInfo['live_id'], $uid)
                            , LiveMessage::MSG_TYPE_POP_ANSWER_REFRESH, [
                                'target' => 'local',
                                'link' => "live_{$answerTypeName}?id={$reqId}&live_record_id={$liveRecordId}&need_auth={$needAuth}",
                                'type' => $type,
                                'force_open' => 0
                            ]);
                    }
                }
                //  当前操作的 申请问题 为当前正在解答的 申请
                $nowAnswerData = $liveAnswerModel->getNowAnswerDataByCache($liveRecordId);
                if ($nowAnswerData && $id === (int)$nowAnswerData['id'])
                {   // 删除当前解答信息
                    $liveAnswerModel->delNowAnswerDataByCache($liveRecordId);
                }
                // 删除当前申请列表
                $liveAnswerModel->delRequestAnswerUserList($this->liveInfo['live_id'], $liveRecordId, $payType, (int)$answerInfo['uid']);
                // 加入已完成列表
                if ($payType === 2)
                {
                    $liveAnswerModel->addResAnswerUserList($liveRecordId, 1, [$answerInfo['id'] => $answerInfo['id']]);
                }
                unset($answerList[$answerInfo['uid']]);
                LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_LIVER, $answerInfo['live_id'], $answerInfo['live_id'])
                    , LiveMessage::MSG_TYPE_POP_LIVE_ANSWER_ICON, [
                        'icon' => "/live/answer/{$answerTypeName}_icon.png",
                        'hint' => '未开始',
                        'type' => $type,
                        'id' => 0,
                        'status' => $answerList ? 1 : 0,
                    ]);

                return show(config('code.success'), '请求成功', ['status' => 1, 'style_type' => $payType]);
            case 3:
                switch ($answerInfo['status'])
                {
                    case 1:
                        return show(config('code.error'), '服务异常，申请解答信息不存在～', ['status' => 2]);
                    case 3:
                        return show(config('code.error'), '等待用户响应中，操作失败～', ['status' => 2]);
                    case 5:
                        return show(config('code.error'), '正在解答中，操作失败～', ['status' => 2]);
                    case 6:
                        return show(config('code.error'), '解答申请已完成，操作失败～', ['status' => 2]);
                    case 7:
                    case 8:
                    case 9:
                        return show(config('code.success'), '解答申请已取消，操作失败～', ['status' => 2]);
                    default:
                }
                $data = config('live_app.live_cancel_list');

                if ($answerInfo['price'] > 0)
                {   // 付费
                    // 更新订单信息 并退款
                    $reason = $data[$reasonId]['name'] ?? '';
                    if (!$reason)
                    {
                        return show(config('code.error'), '取消原因异常，操作失败～', ['status' => 2]);

                    }
                    $res = $liveAnswerModel->refundAnswerOrder(['id' => $answerInfo['id'], 'order_num' => $answerInfo['order_num'], 'status' => $answerInfo['status'], 'uid' => $answerInfo['uid'],], $liveRecordId, 3, $reason);
                    if ($res === false)
                    {
                        return show(config('code.error'), '退款失败');
                    }
                    $payType = 2;//付费
                } else
                {
                    // 更新解答状态
                    $liveAnswerModel->updateAnswerRequest([
                        'id' => $id,
                        'uid' => $answerInfo['uid'],
                        'status' => 9,
                        'cancel_reason' => '',//免费的没有取消原因
                        'cancel' => $this->request->time(),
                        'live_id' => $this->liveInfo['live_id'],
                        'live_record_id' => $liveRecordId,
                        'answer_end_rid' => $liveRecordId
                    ]);

                    $payType = 1;//免费
                }

                //ws 消息
                LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_UID, $this->liveInfo['live_id'], $answerInfo['uid'])
                    , LiveMessage::MSG_TYPE_POP_ANSWER_REFRESH, [
                        'target' => 'local',
                        'link' => "live_{$answerTypeName}?id={$id}&live_record_id={$liveRecordId}&need_auth={$needAuth}",
                        'type' => $type,
                        'force_open' => 1
                    ]);

                $answerList = $liveAnswerModel->getRequestAnswerUserList($this->liveInfo['live_id'], $liveRecordId, $payType);
                foreach ($answerList as $uid => $reqId)
                {
                    if ($uid != $answerInfo['uid'])
                    {
                        // 向直播间其他 申请用户
                        LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_UID, $this->liveInfo['live_id'], $uid)
                            , LiveMessage::MSG_TYPE_POP_ANSWER_REFRESH, [
                                'target' => 'local',
                                'link' => "live_{$answerTypeName}?id={$reqId}&live_record_id={$liveRecordId}&need_auth={$needAuth}",
                                'type' => $type,
                                'force_open' => 0
                            ]);
                    }
                }

                if ($payType === 2)
                {   // 付费
                    // 消息中心添加信息
                    $messageData = [
                        'status' => 0,
                        'is_read' => 0,
                        'range' => $answerInfo['uid'],
                        'type' => 2,
                        'reid' => 0,
                        'cid' => 4,
                        'title' => '解答订单取消',
                        'content' => '非常抱歉，您的' . $answerName . '解答订单：' . $answerInfo['order_num'] . '因主播原因取消，您支付的资金将原路返还，请注意查收
',// 消息中心跳转地址
                        'link' => "order_details?order_id={$answerInfo['order_num']}&order_type={$orderType}",
                        'target' => 'local',
                        'create_time' => time()
                    ];
                    (new MessageCenter())->pushMessage($answerInfo['uid'], $messageData);
                    if (!LiveMessage::getUidIsOnline($this->liveInfo['live_id'], $answerInfo['uid']))
                    {
                        // 直播间不在线  则发送推送
                        // 发送推送
                        (new Push())->pushUserMsg($answerInfo['uid'], '非常抱歉，您的' . $answerName . '解答订单：' . $answerInfo['order_num'] . '因主播原因取消，您支付的资金将原路返还，请注意查收
', ['action' => 'extend', 'target' => 'local', 'link' => "order_details?order_id={$answerInfo['order_num']}&order_type={$orderType}"]);
                    }
                }

                //  当前操作的 申请问题 为当前正在解答的 申请
                $nowAnswerData = $liveAnswerModel->getNowAnswerDataByCache($liveRecordId);
                if ($nowAnswerData && $id === (int)$nowAnswerData['id'])
                {   // 删除当前解答信息
                    $liveAnswerModel->delNowAnswerDataByCache($liveRecordId);
                }
                // 删除当前申请列表
                $liveAnswerModel->delRequestAnswerUserList($this->liveInfo['live_id'], $liveRecordId, $payType, (int)$answerInfo['uid']);
                // 加入 取消 列表
                if ($payType === 2)
                {
                    $liveAnswerModel->addResAnswerUserList($liveRecordId, 2, [$answerInfo['id'] => $answerInfo['id']]);
                }

                unset($answerList[$answerInfo['uid']]);
                LiveMessage::sendMsg(LiveMessage::who(LiveMessage::WHO_TYPE_LIVER, $answerInfo['live_id'], $answerInfo['live_id'])
                    , LiveMessage::MSG_TYPE_POP_LIVE_ANSWER_ICON, [
                        'icon' => "/live/answer/{$answerTypeName}_icon.png",
                        'hint' => '未开始',
                        'type' => $type,
                        'id' => 0,
                        'status' => $answerList ? 1 : 0,
                    ]);

                return show(config('code.success'), '请求成功', ['status' => 1, 'style_type' => $payType]);
            default:
        }
        return show(config('code.error'), '服务异常，请稍后再试～', ['status' => 2]);
    }

    /**
     * 获取互动功能信息
     * @return Json
     * @throws \Exception
     */
    public function answerInteractionInfo()
    {
        $liveRecordId = (new LiveRecordingInfo())->getNewestRecord($this->liveInfo['live_id']);
        $astroInfo = (new LiveAnswerAstroRecord())->getLiveManageAnswerProgressInfo($liveRecordId, 1);
        $diceInfo = (new LiveAnswerDiceRecord())->getLiveManageAnswerProgressInfo($liveRecordId, 1);
        $tarotInfo = (new LiveAnswerTarotRecord())->getLiveManageAnswerProgressInfo($liveRecordId, 1);
        $data = array_merge($astroInfo, $diceInfo, $tarotInfo);
        return show(config('code.success'), '获取成功', $data);
    }

    /**
     * 获取看盘详情
     * @return Json
     * @throws ApiException
     */
    public function getAstroAnswerDetail()
    {
        $id = input('id/d', 0);
        $data = $this->getAnswerDetail($id, 1);
        $recode = json_decode($data['extend'], true);
        $recodeList = [];
        if (isset($recode['recode1']) && $recode['recode1'])
        {
            $recodeList[] = [
                'id' => $recode['recode1']['id'],
                'name' => $recode['recode1']['name'],
                'type' => $recode['recode1']['type'] == 1 ? 1 : 2,
                'age' => '年龄' . birthday($recode['recode1']['year'] . '-' . $recode['recode1']['month'] . '-' . $recode['recode1']['day']),
                'comments' => $recode['recode1']['comments']
            ];
        }
        if (isset($recode['recode2']) && $recode['recode2'])
        {
            $recodeList[] = [
                'id' => $recode['recode2']['id'],
                'name' => $recode['recode2']['name'],
                'type' => $recode['recode2']['type'] == 1 ? 1 : 2,
                'age' => '年龄' . birthday($recode['recode2']['year'] . '-' . $recode['recode2']['month'] . '-' . $recode['recode2']['day']),
                'comments' => $recode['recode2']['comments']
            ];
        }

        //星盘地址
        $astro_url = Util::getAstroUrlFromRecords($this->request->header(), $recode);

        $res = [
            'recode_list' => $recodeList,
            'question' => $data['question'],
            'question_des' => $data['question_des'],
            'status' => $data['status'],
            'continue_sencond' => $data['continue_sencond'],
            'uen' => $data['uen'],
            'style_type' => (float)$data['price'] > 0 ? 2 : 1,//1=>免费。2=>付费
            'astro_url' => $astro_url,//星盘地址
        ];
        return show(config('code.success'), '获取成功', $res);
    }

    /**
     * 获取骰子详情
     * @return Json
     * @throws ApiException
     */
    public function getDiceAnswerDetail()
    {
        $id = input('id/d', 0);
        $data = $this->getAnswerDetail($id, 2);
        $history = (new Divination())->getDetail(['id' => $data['history_id']]);
        $user = (new User())->getUserinfo(['id' => $data['uid']]);
        $res = [
            'question' => $data['question'],
            'planet' => $history['planet'],
            'sign' => $history['sign'],
            'house' => $history['house'],
            'name' => $user['nickname'],
            'avatar' => $user['avatar'],
            'status' => $data['status'],
            'continue_sencond' => $data['continue_sencond'],
            'uen' => $data['uen'],
            'style_type' => (float)$data['price'] > 0 ? 2 : 1,//1=>免费。2=>付费
        ];
        return show(config('code.success'), '获取成功', $res);
    }

    /**
     * 获取塔罗详情
     * @return Json
     * @throws ApiException
     */
    public function getTarotAnswerDetail()
    {
        $id = input('id/d', 0);
        $data = $this->getAnswerDetail($id, 3);
        $history = (new ZlTarotRecords())->getInfoById($data['history_id']);
        $detail = (new ZlTarotRecords())->getDetails($history);
        $user = (new User())->getUserinfo(['id' => $data['uid']]);
        $res = [
            'question' => $data['question'],
            'name' => $user['nickname'],
            'avatar' => $user['avatar'],
            'status' => $data['status'],
            'continue_sencond' => $data['continue_sencond'],
            'uen' => $data['uen'],
            'tarot_cards' => $detail['tarot_cards'],
            'style_type' => (float)$data['price'] > 0 ? 2 : 1,//1=>免费。2=>付费
        ];
        return show(config('code.success'), '获取成功', $res);
    }

    /**
     * 获取解答详情
     * @param $id
     * @param $type
     * @return mixed
     * @throws ApiException
     */
    protected function getAnswerDetail($id, $type)
    {
        $liveRecordId = (new LiveRecordingInfo())->getNewestRecord($this->liveInfo['live_id']);
        $answerModel = null;
        switch ($type)
        {
            case 1:
                $answerModel = new LiveAnswerAstroRecord();
                break;
            case 2:
                $answerModel = new LiveAnswerDiceRecord();
                break;
            case 3:
                $answerModel = new LiveAnswerTarotRecord();
                break;
        }
        $answerInfo = $answerModel->getInfoById($id);
        if (!$answerInfo || $answerInfo['status'] == 1 || $answerInfo['live_id'] != $this->liveInfo['live_id'])
        {
            throw new ApiException('解答不存在');
        }

        //解答状态 1=>待解答，2=>待响应，3=>解答中 4=>已完成 5=>已取消
        $timeraning = 0;
        switch ($answerInfo['status'])
        {
            case 2:
            case 4:
                $answerInfo['status'] = 1;
                break;
            case 3:
                $answerInfo['status'] = 2;
                //获取当前看盘数据
                $nowData = $answerModel->getNowAnswerDataByCache($liveRecordId);
                $timeraning = $nowData['request_time'] + 30 - time();
                break;
            case 5:
                $answerInfo['status'] = 3;
                break;
            case 6:
                $answerInfo['status'] = 4;
                break;
            default:
                $answerInfo['status'] = 5;
                break;
        }

        $answerInfo['continue_sencond'] = $timeraning > 0 ? $timeraning : 0;
        $answerInfo['uen'] = (new User())->getUserinfo(['id' => $answerInfo['uid']])['uen'];
        return $answerInfo;
    }
}
